<template>
  <div>
      <a-input-search
      placeholder="请输入小说名称"
      @search="onSearch"
      enterButton="Search"
      size="large"
    />
    <a-button @click="stop">停止搜索</a-button>
    <a-table :columns="columns" :dataSource="dataSource"></a-table>
  </div>
</template>

<script>
import { searchNovel, stopSearchNovel, downloadNovel } from '@/api'
import {
  CALLBACK,
  SEARCH_NOVEL
} from 'novel-parser/servies/channel'
export default {
  data () {
    return {
      searchKey: '',
      columns: [{ title: '链接', dataIndex: 'bookUrl' },
        { title: '封面链接', dataIndex: 'coverUrl' },
        { title: '种类', dataIndex: 'kind' },
        { title: '最后章节', dataIndex: 'lastChapter' },
        { title: '名字', dataIndex: 'name' } ],
      dataSource: []
    }
  },
  mounted () {
    this.$electron.ipcRenderer.on(SEARCH_NOVEL + CALLBACK, (event, result) => {
      console.log(result)
      if (result.code === 200) {
        const list = result.data.result
        this.dataSource.push(...list)
      } else {
        console.log(result)
      }
    })
  },
  methods: {
    onSearch (e) {
      if (e.startsWith('http')) {
        downloadNovel(e).then(result => {
          this.$router.push(`/novel/${result._id}`)
        }).catch(err => {
          this.$message.error(err.message)
        })
      } else {
        this.$electron.ipcRenderer.send(SEARCH_NOVEL, { searchKey: e })
      }
    },
    stop () {
      stopSearchNovel().then(console.log)
    }
  }
}
</script>

<style>

</style>